package com.linkoog.devtools.intellij.ui.list;



import com.intellij.openapi.util.TextRange;
import com.intellij.ui.CellRendererPanel;
import com.intellij.ui.JBColor;
import com.intellij.ui.SimpleColoredComponent;
import com.intellij.ui.SimpleTextAttributes;
import com.intellij.ui.speedSearch.SpeedSearchUtil;
import com.intellij.util.ui.JBUI;
import com.intellij.util.ui.UIUtil;

import javax.swing.*;
import java.awt.*;
import java.util.List;

/**
 *   list的左右渲染器，支持颜色渲染
 */
public class RightLeftListCellRenderer implements ListCellRenderer<Object> {
    private SimpleTextAttributes myHighlighted = new SimpleTextAttributes(UIUtil.getListBackground(), UIUtil.getListForeground(), null, SimpleTextAttributes.STYLE_SEARCH_MATCH);
    private SimpleTextAttributes mySelected = new SimpleTextAttributes(UIUtil.getListSelectionBackground(true), UIUtil.getListSelectionForeground(true),
            JBColor.RED, SimpleTextAttributes.STYLE_PLAIN);

    private SimpleTextAttributes myPlain = new SimpleTextAttributes(UIUtil.getListBackground(), UIUtil.getListForeground(), JBColor.RED, SimpleTextAttributes.STYLE_PLAIN);

    private SimpleColoredComponent myLeftRenderer = new SimpleColoredComponent();
    private SimpleColoredComponent myRightRenderer = new SimpleColoredComponent();

    private CellRendererPanel myPanel = new CellRendererPanel(new BorderLayout());

    public RightLeftListCellRenderer() {
        myLeftRenderer.setOpaque(false);
        myRightRenderer.setOpaque(false);
        myPanel.setOpaque(true);
        myPanel.add(myLeftRenderer, BorderLayout.WEST);
        myPanel.add(myRightRenderer, BorderLayout.EAST);
        myPanel.setBorder(JBUI.Borders.empty(5));
    }

    @Override
    public Component getListCellRendererComponent(JList<? extends Object> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
        myLeftRenderer.clear();
        myLeftRenderer.setIcon(null);
        List<? extends TextRange> textRanges = List.of();
        SpeedSearchUtil.appendColoredFragments(myLeftRenderer, "PlainText", textRanges,
                (isSelected ? mySelected : myPlain), (isSelected ? myHighlighted : myPlain));

        myRightRenderer.clear();
        myRightRenderer.append("");
        myRightRenderer.setForeground(isSelected ? UIUtil.getActiveTextColor() : UIUtil.getInactiveTextColor());

        myPanel.setBackground(isSelected ? UIUtil.getListSelectionBackground(true) : UIUtil.getListBackground());
        return myPanel;
    }
}
